package com.changgou.footmark.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.footmark.pojo.Footmark;
import com.changgou.footmark.service.FootmarkService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/footmark")
@CrossOrigin
public class FootmarkController {

    @Autowired
    private FootmarkService footmarkService;

    /***
     * Footmark分页条件搜索实现
     * @param footmark
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Footmark footmark, @PathVariable(value = "page")  int page, @PathVariable(value = "size")  int size){
        //调用FootmarkService实现分页条件查询Footmark
        PageInfo<Footmark> pageInfo = footmarkService.findPage(footmark, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Footmark分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable(value = "page")  int page, @PathVariable(value = "size")  int size){
        //调用FootmarkService实现分页查询Footmark
        PageInfo<Footmark> pageInfo = footmarkService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param footmark
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Footmark>> findList(@RequestBody(required = false) Footmark footmark){
        //调用FootmarkService实现条件查询Footmark
        List<Footmark> list = footmarkService.findList(footmark);
        return new Result<List<Footmark>>(true, StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable(value = "id") Integer id){
        //调用FootmarkService实现根据主键删除
        footmarkService.delete(id);
        return new Result(true, StatusCode.OK,"删除成功");
    }

    /***
     * 修改Footmark数据
     * @param footmark
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Footmark footmark, @PathVariable(value = "id") Integer id){
        //设置主键值
        footmark.setId(id);
        //调用FootmarkService实现修改Footmark
        footmarkService.update(footmark);
        return new Result(true, StatusCode.OK,"修改成功");
    }

    /***
     * 新增Footmark数据
     * @param footmark
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Footmark footmark){
        //调用FootmarkService实现添加Footmark
        footmarkService.add(footmark);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Footmark数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Footmark> findById(@PathVariable(value = "id") Integer id){
        //调用FootmarkService实现根据主键查询Footmark
        Footmark footmark = footmarkService.findById(id);
        return new Result<Footmark>(true, StatusCode.OK,"查询成功",footmark);
    }

    /***
     * 查询Footmark全部数据
     * @return
     */
    @GetMapping
    public Result<List<Footmark>> findAll(){
        //调用FootmarkService实现查询所有Footmark
        List<Footmark> list = footmarkService.findAll();
        return new Result<List<Footmark>>(true, StatusCode.OK,"查询成功",list) ;
    }

    /**
     * 根据用户名查询对应的全部浏览数据
     */
    @RequestMapping(value = "/list")
    public Result<List<Footmark>> findByUsername(){
        // TODO 从令牌中获取用户名(优化)
        String username = "zhangsan";
        List<Footmark> list = footmarkService.findByUsername(username);
        return new Result<List<Footmark>>(true, StatusCode.OK,"查询成功",list) ;
    }

    /**
     * 根据skuId增加浏览记录
     * @param id 商品skuId
     */
    @RequestMapping(value = "/add")
    public Result addBySkuId(Long id){
        // TODO 从令牌中获取用户名(优化)
        String username = "zhangsan";
        // 调用业务层新增记录
        footmarkService.addBySkuId(id, username);
        return new Result(true, StatusCode.OK,"添加成功");
    }

}
